// 2025/3/7
// https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
// 反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(head == nullptr)
                return nullptr;
            ListNode *pre = nullptr, *cur = head, *next = head->next;
            while(next != nullptr)
            {
                cur->next = pre;
                pre = cur;
                cur = next;
                next = cur->next;
            }
            cur->next = pre;
            return cur;
        }
    };